﻿
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using System.Text;
using Gift.WebApp;
using Gift.Model;


    public partial class admin_product_cardQuery : BaseAdmin
    {
        private void GridViewBind(string Sqlsort)
        {
            string tempCompanyCode = string.Empty;
            if (this.cardcode.Value.Trim().Length > 4)
            {
                tempCompanyCode = this.cardcode.Value.Trim().Substring(0, 4);

                string sql = string.Format("select distinct C.CardCode,C.CardPrice,C.states,(select [name] from tbCardState where id=C.states) as statesName,0 as userType, '订单号为：' +  Convert(varchar(18),O.OrderId) as code  "
                      + " FROM tbCard{0} C  "
                      + "  join tbOrderAccount OA on C.Cardid = OA.Cardid "
                      + "  join tbCardOrder O on OA.CardId = O.CardId "
                      + " where c.CardCode = '" + this.cardcode.Value.Trim() + "' and C.states=2", tempCompanyCode);
                string sql2 = string.Format(" select distinct C.CardCode,C.CardPrice,C.states,(select [name] from tbCardState where id=C.states) as statesName,1 as userType,MA.MemberId  as id, "
                            + "  '用户名为：' +(select memberlogin from DB_Member where MemberId = MA.MemberId) as code "
                           + " FROM tbCard{0} C "
                           + "  join tbMemberAccount MA on C.Cardid = MA.Cardid "
                           + " where c.CardCode = '" + this.cardcode.Value.Trim() + "' and C.states=2", tempCompanyCode);
                string sql3 = string.Format(" select *,(select [name] from tbCardState where id=tbCard.states) as statesName,2 as userType,'' as code from tbCard{0} "
                              + " where CardCode = '" + this.cardcode.Value.Trim() + "'", tempCompanyCode);

                DataTable table = new DataTable();
                table = SQLAccess.GreatDs(sql).Tables[0];
                if (table.Rows.Count <= 0)
                {
                    table = SQLAccess.GreatDs(sql2).Tables[0];
                }
                if (table.Rows.Count <= 0)
                {
                    table = SQLAccess.GreatDs(sql3).Tables[0];
                }
                this.GridView1.DataSource = table.DefaultView;
                this.GridView1.DataBind();
            }
           
          
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {       
                base.ValidateLoginRule(ERule.CardQuery);
                this.GridViewBind("");

            }
        }


        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind("");
        }

    }